package com.gmics.unifiq;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.gmics.android.R;
import com.gmics.unifiq.controller.EntityController;
import com.gmics.unifiq.entity.User;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MemberDetail extends Activity {

	Intent i;
	ImageView profilePic;
	TextView name, school, birthday, email;
	int uid;
	ProgressBar progBar;
	EntityController ec = new EntityController();
	User user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_member_detail);
		
		// Initialize controls
		profilePic = (ImageView) findViewById(R.id.profilePic);
		name = (TextView) findViewById(R.id.name);
		school = (TextView) findViewById(R.id.school);
		birthday = (TextView) findViewById(R.id.birthday);
		progBar = (ProgressBar) findViewById(R.id.progBar);
		email = (TextView) findViewById(R.id.email);

		// get intent values
		i = getIntent();
		uid = i.getExtras().getInt("userID");
		new getUserDetails().execute();


	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.member_detail, menu);
		return true;
	}
	
	private class MyNetworkTask extends AsyncTask<URL, Void, Bitmap> {

		ImageView tIV;
		ProgressBar pb;

		public MyNetworkTask(ImageView iv, ProgressBar progBar) {
			tIV = iv;
			pb = progBar;
		}// constructor

		@Override
		protected Bitmap doInBackground(URL... urls) {

			Bitmap networkBitmap = null;

			URL networkUrl = urls[0]; // Load the first element
			try {
				networkBitmap = BitmapFactory.decodeStream(networkUrl
						.openConnection().getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}

			return networkBitmap;
		}// return bitmap

		@Override
		protected void onPostExecute(Bitmap result) {
			
			tIV.setImageBitmap(result);
			tIV.setVisibility(View.VISIBLE);
			pb.setVisibility(View.GONE);
			
		}// hide progress bar

	}

	private class getUserDetails extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			user = ec.retrieveUser(uid);			
			return null;
		}

		@Override
		protected void onPostExecute(String result) {

			// set texts
			name.setText(user.getName());
			school.setText(user.getSchool());
			birthday.setText(user.getBirthday());
			email.setText(user.getEmail());

			// Load bitmap from internet
			URL onLineURL;
			try {
				onLineURL = new URL(user.getProfilePic());
				new MyNetworkTask(profilePic, progBar).execute(onLineURL);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			
			super.onPostExecute(result);
		}

		

	}
	
	

}
